// StringTools.h
//
#ifndef STRINGTOOLS_ONCE
#define STRINGTOOLS_ONCE

#include <string>
#include <vector>

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)

// ------------------------------------------------------------------------------------------------
inline std::vector<std::string> split( const std::string &str, const std::string &delimiters )
{
	std::vector<std::string> out;

	std::size_t start = 0;
	std::size_t pos   = str.find_first_of( delimiters, start );
	
	while( pos != std::string::npos )
	{
		if( pos != start )
			out.push_back( str.substr(start,pos-start) );
		
		start = pos+1;
		pos=str.find_first_of(delimiters,start);
	}
	
	if( start<str.length() )
		out.push_back( str.substr(start) );

	return out;
}

// ------------------------------------------------------------------------------------------------
inline bool startsWith( const std::string &str, const std::string &key )
{
	const std::string left = str.substr( 0,key.size() );

	return left == key;
}

// ------------------------------------------------------------------------------------------------
inline bool endsWith( const std::string &str, const std::string &key )
{
	if( str.size() < key.size() )
		return false;

	const std::string right = str.substr( str.size()-key.size(),key.size() );

	return right == key;
}

// ------------------------------------------------------------------------------------------------
inline std::string after( const std::string &str, const std::string &delimiter )
{
	const size_t pos = str.find_first_of( delimiter )+1;

	std::string ret = str.substr( pos, std::string::npos );

	return ret;
}

// ------------------------------------------------------------------------------------------------
inline std::string fromWString( const std::wstring &str )
{
	int sizeOfWChar = sizeof(wchar_t);
	char* c = new char[sizeOfWChar];

	std::vector<char> buf;
	for (size_t i=0; i<str.size(); i++) {
		wchar_t wc = str.at(i);
		int size;
		wctomb_s(&size, c, sizeOfWChar, wc);
		buf.push_back( c[0] );
	}

	std::string ret = "";
	ret.assign(buf.begin(), buf.end());	

	delete c;
	return ret;
}

// ------------------------------------------------------------------------------------------------
inline std::wstring toWString( const std::string &str )
{
	int sizeOfWChar = sizeof(wchar_t);
	wchar_t wc;

	std::vector<wchar_t> buf( str.size() );
	for (size_t i=0; i<str.size(); i++) {
		mbtowc(&wc, &str.at(i), 1);
		buf.push_back( wc );
	}

	std::wstring ret = L"";
	ret.assign(buf.begin(), buf.end());	

	return ret;
}

#endif